import { Toast } from 'vant'
import { inject, reactive, computed } from 'vue'
import {compareVersion} from '@@/utils/common.js'
import {useRouter} from 'vue-router'

function useSettings(){
    const router = useRouter()
    const state = reactive({
        permission:0,
        showUpdate:false
    })
    const vueState = inject('vueState')
    const needUpdate = computed(()=>{
        const {version,lastestVersion} = vueState.versionControl
        if(lastestVersion === ''){
            return false
        }else if(version === lastestVersion){
            return false
        }else{
            return compareVersion(version || '',lastestVersion || '')
        }
    })
    const clickUpdate = ()=> {
        if(needUpdate.value){
            window.trackEvent({ev:'clickUpdateTab'})
            // console.log(vueState.versionControl.lastestVersion);
            updateModalData.version = vueState.versionControl.lastestVersion
            // console.log(vueState.versionControl.lastestVersion,updateModalData);
            updateModalData.showModal()
        }else{
            Toast('The Latest version')
        }
    }
    const clickAbout = ()=> {
        window.trackEvent({ev:'clickAboutTab'})
        router.push({name:'about'})
    }
    const setUpdateShow = ()=> {
        if(vueState.pageInfo.channel.indexOf('Google') > -1){
            state.showUpdate = false
        }else{
            state.showUpdate = true
        }
    }
    const updateModalData = reactive({
        show:false,
        showModal:()=>{
            updateModalData.show = true
        },
        version:'',
        submit:()=>{
            window.trackEvent({ev:'clickUpdateButton',remarks:`Version:V${vueState.versionControl.lastestVersion}`})
            window.SDKPostMessage({
                event:'h5_download_apk',
                data:{
                    icon:vueState.versionControl.icon,
                    title:vueState.versionControl.appName,
                    bundle:vueState.bundle,
                    url:vueState.versionControl.downloadUrl
                }
            })
        }
    })
    const onSwitchUpdate = ()=> {
        window.trackEvent({ev:'clickReminderTab'})
        window.SDKPostMessage({
            event:'h5_permission_set',
            data:{
                permission:['push']
            }
        })
    }
    const getPermissionInfo = (res)=> {
        console.log('getPermissionInfo',res);
        const {push} = res.data
        state.permission = push
    }
    return {
        state,
        updateModalData,
        getPermissionInfo,
        onSwitchUpdate,
        needUpdate,
        clickUpdate,
        clickAbout,
        setUpdateShow
    }
}
export {useSettings}